﻿$(function(){

	if($.browser.mozilla) {
	$('.mdetail thead td').css('height','69px')
	}
	else if($.browser.safari){
	$('.mdetail thead td').css('height','60px')
	}
 /*correct FF&safari month detail td height*/
 
	$('.info td').dblclick(function(){
		location.replace('viewDetails.html');
	})
	
	$('.cfnav li').click(function(){
		$('.cfnav li').removeClass('current');
		$(this).addClass('current');
	})
	
	$('.year').focus(function(){
		$(this).attr('value','')
	})
	$('.year').blur(function(){
		if(!$(this).attr('value')){
			$(this).attr('value','please input the year')
		}
	})

	$('.today').datepicker();
	$('.expiring').datepicker();
	$('.date').datepicker();

	
	var input = $('.filter dt input')
	input.click(function(){
		if($(this).attr('checked')){
			$(this).parents('dl').find('dd input').attr('checked','true');
		}
		else{
			$(this).parents('dl').find('dd input').removeAttr('checked');
		}
	})
	
		/*datepicker*/
		var dates = $( "input[mark='f'] , input[mark='t']" ).datepicker({
			//defaultDate: "+1w",
			//changeMonth: true, 
			//numberOfMonths: 3,
			onSelect: function( selectedDate ) {
			    /*if select by class*/
				//var thisClass = $(this).attr('class');
				//var reg = /from/gi;
				//var option = this.class = reg.test(thisClass) ? "minDate" : "maxDate",
				var option = this.name == "from" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" ),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			}	
		});	

	/*leave apply hide and show*/

	$('.applyform dt').click(function(){
	
		$('.applyform dd input').removeAttr('class');
		$(this).next().find('input[mark="f"]').addClass('from');
		$(this).next().find('input[mark="t"]').addClass('to');
		$('.arrow1').removeClass('arrow2');
		$('.applyform dd').hide();
		$('.applyform dt input').removeAttr('checked');
		$(this).next().show().end().find('input').attr('checked','checked').end().find('.arrow1').addClass('arrow2');
		
			
			/*apply leave datepicker*/
		var dates = $( ".from , .to" ).datepicker({
			//defaultDate: "+1w",
			//changeMonth: true, 
			//numberOfMonths: 3,
			onSelect: function( selectedDate ) {
				var thisClass = $(this).attr('class');
				var reg = /from/gi;
				var option = this.name = reg.test(thisClass) ? "minDate" : "maxDate",

					instance = $( this ).data( "datepicker" ),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			}	
		});	
	})

	/*validation*/
	$('.btngroup .submit').click(function(e){
		$('.message').remove();
		$.each($('.applyform dd'),function(index,value){
			if(!$(this).is(':visible')){
				return;
				}
			else if(!($('.from').val() && $('.to').val())){
				$(this).children('.total').before('<span class="message">please choose the date</span>')
				}
			else if($('.from').val()>$('.to').val()){
				$(this).children('.total').before('<span class="message">from date is later than to date </span>')
			}
		})
	})

	

})